<template>
  <div class="mod-role">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.zbName" placeholder="指标名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addHandle()">新增</el-button>
        <el-button type="primary" @click="addtest()">测试数据新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="zbCode"
        header-align="center"
        align="center"
        width="80"
        label="指标编码">
      </el-table-column>
      <el-table-column
        prop="minValue"
        header-align="center"
        align="center"
        label="最小值">
      </el-table-column>
      <el-table-column
        prop="maxValue"
        header-align="center"
        align="center"
        label="最大值">
      </el-table-column>
      <el-table-column
        prop="zbLateValue"
        header-align="center"
        align="center"
        width="180"
        label="最新指标值">
      </el-table-column>
      <el-table-column
        prop="zbAvgValue"
        header-align="center"
        align="center"
        width="180"
        label="平均指标值">
      </el-table-column>
      <el-table-column
        prop="zbMinValue"
        header-align="center"
        align="center"
        width="180"
        label="指标最小值">
      </el-table-column>
      <el-table-column
        prop="zbMaxValue"
        header-align="center"
        align="center"
        width="180"
        label="指标最大值">
      </el-table-column>
      <el-table-column
        prop="zbLevel"
        header-align="center"
        align="center"
        width="180"
        label="等级">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="180"
        label="状态">
      </el-table-column>
       <el-table-column
        fixed="right"
        header-align="center"
        width="150"
        label="操作">
         <template slot-scope="scope">
           <el-button type="text" size="small">开启</el-button>
           <el-button type="text" size="small" @click="updateHandle(scope.row.zbCode)">修改</el-button>
           <el-button type="text" size="small" @click="deleteHandle(scope.row.zbCode)">删除</el-button>
         </template>
       </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <zbconfig-add v-if="addVisible" ref="add" @refreshDataList="getDataList"></zbconfig-add>
    <zbconfig-update v-if="updateVisible" ref="update" @refreshDataList="getDataList"></zbconfig-update>
    <zbconfig-test v-if="testVisible" ref="test" @refreshDataList="getDataList"></zbconfig-test>
  </div>
</template>

<script>
  import ZbconfigAdd from './zbconfig-add'
  import ZbconfigUpdate from './zbconfig-update'
  import ZbconfigTest from './zbconfig-test'
  import API from '@/api'
  export default {
    data () {
      return {
        dataForm: {
          zbName: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        addVisible: false,
        updateVisible: false,
        testVisible: false
      }
    },
    components: {
      ZbconfigAdd,
      ZbconfigUpdate,
      ZbconfigTest
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        var params = {
          page: this.pageIndex,
          limit: this.pageSize,
          zbName: this.dataForm.zbName
        }
        API.zbconfig.zbconfiglist(params).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增
      addHandle () {
        this.addVisible = true
        this.$nextTick(() => {
          this.$refs.add.init()
        })
      },
      addtest () {
        this.testVisible = true
        this.$nextTick(() => {
          this.$refs.test.init()
        })
      },
      updateHandle (id) {
        this.updateVisible = true
        this.$nextTick(() => {
          this.$refs.update.init(id)
        })
      },
      // 删除
      deleteHandle (zbCode) {
        var params = {
          'zbCode': zbCode,
          'creator': this.$store.state.user.name
        }
        API.zbconfig.del(params).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
